package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CancelSendTransport;
import com.hzhc.entity.CntrInfo;
import com.hzhc.enumBean.LoginUserType;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.enumBean.SendTransportState;
import com.hzhc.mapper.CancelSendTransportMapper;
import com.hzhc.mapper.CntrInfoMapper;
import com.hzhc.service.CancelSendTransportService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.CancelSendTransportVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 取消发运 服务实现类
 * </p>
 *
 * @author shizj
 * @since 2024-09-02
 */
@Service
@RequiredArgsConstructor
public class CancelSendTransportServiceImpl extends ServiceImpl<CancelSendTransportMapper, CancelSendTransport> implements CancelSendTransportService {
    private final CntrInfoMapper cntrInfoMapper;

    @Override
    public List<CancelSendTransportVo> getList(Page<CancelSendTransportVo> page, CancelSendTransportVo vo) {
        IPage<CancelSendTransportVo> iPage = baseMapper.selectCancelSendTransportVoList(page,vo);
        return iPage.getRecords();
    }

    @Override
    public ResultUtil<T> addCancelSendTransport(UserDto user, CntrInfo info, String code) {
        CntrInfo cntrInfo = cntrInfoMapper.selectById(info.getCntrNo());
        if(!cntrInfo.getSendTransportState().equals(info.getSendTransportState())){
            return ResultUtil.build(400, ResultUtilCodeEnum.SENDTRANSPORTSTATE_CHANGED.getMessage());
        }
        if (cntrInfo.getStatus() != 0) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_NOTIN_YARD);
        }
        CancelSendTransport cancelSendTransport = new CancelSendTransport();
        Integer applySendTransportState = cntrInfo.getSendTransportState().equals(SendTransportState.CONTINUE_SEND_TRANSPORT.getCode())?
                SendTransportState.CANCEL_SEND_TRANSPORT.getCode():SendTransportState.CONTINUE_SEND_TRANSPORT.getCode();
        cancelSendTransport.setCntrNo(cntrInfo.getCntrNo()).setApplySendTransportState(applySendTransportState);
        if(LoginUserType.USER.getCode().equals(code)){
            cancelSendTransport.setUserId(user.getId()).setCreateName(user.getRealname());
        }else if(LoginUserType.MEMBER.getCode().equals(code)){
            cancelSendTransport.setMemberId(user.getId()).setCreateName(user.getRealname());
        }
        boolean b = save(cancelSendTransport);
        cntrInfo.setSendTransportState(applySendTransportState);
        cntrInfoMapper.updateById(cntrInfo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateCheck(UserDto user, CancelSendTransportVo vo) {
        CancelSendTransport cancelSendTransport = baseMapper.selectById(vo.getId());
        CntrInfo cntrInfo = cntrInfoMapper.selectById(cancelSendTransport.getCntrNo());
        if(cancelSendTransport.getAuditFlag()!=0){
            return ResultUtil.build(400, ResultUtilCodeEnum.AUDITFLAG_CHANGED.getMessage());
        }
        if (cntrInfo.getStatus() != 0) {
            return ResultUtil.build(ResultUtilCodeEnum.CNTR_NOTIN_YARD);
        }
        if(cntrInfo.getSendTransportState().equals(cancelSendTransport.getApplySendTransportState())){
            return ResultUtil.build(400, "集装箱发运状态已经是"+
                    SendTransportState.CANCEL_SEND_TRANSPORT.getNameByCode(cancelSendTransport.getApplySendTransportState())
                    +",无需审核修改");
        }
        cancelSendTransport.setAuditUserId(user.getId()).setAuditUserName(user.getRealname()).setAuditFlag(1).setAuditTime(LocalDateTime.now())
                .setUpdateUserId(user.getId()).setUpdateName(user.getRealname());
        boolean b = updateById(cancelSendTransport);
        //修改集装箱发运状态
        cntrInfo.setSendTransportState(cancelSendTransport.getApplySendTransportState());
        cntrInfoMapper.updateById(cntrInfo);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateRefugee(UserDto user, CancelSendTransportVo vo) {
        CancelSendTransport cancelSendTransport = baseMapper.selectById(vo.getId());
        if(cancelSendTransport.getAuditFlag()!=0){
            return ResultUtil.build(400, ResultUtilCodeEnum.AUDITFLAG_CHANGED.getMessage());
        }
        cancelSendTransport.setAuditUserId(user.getId()).setAuditUserName(user.getRealname()).setAuditFlag(2).setAuditTime(LocalDateTime.now())
                .setRefugeeReason(vo.getRefugeeReason()).setUpdateUserId(user.getId()).setUpdateName(user.getRealname());
        boolean b = updateById(cancelSendTransport);
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }
}
